﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.Interface;
using Cspl.AnimalFight.Lib.EventHandlers;

namespace Cspl.AnimalFight.Lib.Data
{
    public class Snake : AnimalBase, IAttack, IDefend, IAttackable
    {

        public event AttackDelegate AttackEvent;
        public event DefendDelegate DefendEvent;

        public bool isDefending { get; set; }

        public void Attack(string targetName)
        {
            if (this.IsDead)
            {
                Console.WriteLine("{0} is already dead. Cannot attack other animals.", this.Name);
            }
            else if (this.HasEscape)
            {
                Console.WriteLine("{0} has escaped. Cannot attack other animals.", this.Name);
            }
            else
            {
                Console.WriteLine("{0} is attacking {1}.", this.Name, targetName);

                if (AttackEvent != null)
                {
                    AttackEventHandler e = new AttackEventHandler
                    {
                        AnimalTargetName = targetName,
                        AP = this.AP
                    };

                    AttackEvent(this, e);
                }
            }
        }

        public void Defend()
        {
            if (DefendEvent != null)
            {
                DefendEvent(this, new DefendEventHandler 
                {
                    AnimalName = this.Name,
                    Target = Animals.Snake
                });
            }
        }

        public void Attacked(int ap)
        {
            if (this.HasEscape)
            {
                Console.WriteLine("The {0} has escaped. Cannot be attacked.", this.Name);
            }
            else if (this.IsDead)
            {
                Console.WriteLine("The {0} is already dead. Cannot be attacked.", this.Name);
            }
            else
            {
                Console.WriteLine("{0} is being attacked. Current HP = {1}", this.Name, this.HP);

                int realDp = this.isDefending ? this.DP + 10 : this.DP;
                
                this.HP -= (ap - realDp);

                Console.WriteLine("{0} has been attacked. -{1}", this.Name, ap - realDp);
                Console.WriteLine("Current HP = {0}", this.HP);

                if (this.HP <= 0)
                {
                    this.IsDead = true;
                    Console.WriteLine("{0} is dead.", this.Name);
                }
            }
        }

        public override void Escape()
        {
            this.HasEscape = true;
            Console.WriteLine("{0} has escaped.", this.Name);
        }

    }
}
